import Vue from 'vue'
import VueRouter from 'vue-router'

Vue.use(VueRouter)

const routes = [{
    path: '/',
    redirect: '/login'
  },
  {
    path: '/login',
    name: 'login',
    // route level code-splitting
    // this generates a separate chunk (about.[hash].js) for this route
    // which is lazy-loaded when the route is visited.
    component: () => import('../components/login.vue')
  },
  {
    path: '/home',
    name: 'home',
    component: () => import('../components/home.vue'),
    redirect: '/welcome',
    children: [{
        path: '/welcome',
        name: 'welcome',
        component: () => import('../components/welcome.vue')
      }, {
        path: '/aboutme',
        name: 'aboutme',
        component: () => import('../components/aboutme.vue')
      },
      {
        path: '/userlist/:menuId',
        name: 'userlist',
        component: () => import('../components/user/userlist.vue')
      },
      {
        path: '/rolelist/:menuId',
        name: 'rolelist',
        component: () => import('../components/rights/rolelist.vue')
      },
      {
        path: '/rightlist/:menuId',
        name: 'rightlist',
        component: () => import('../components/rights/rightlist.vue')
      }
    ]
  }
]

const router = new VueRouter({
  routes
})

router.beforeEach((to, from, next) => {
  if (to.path === '/login') return next();
  const token = window.sessionStorage.getItem('token');
  if (!token) return next('/login')
  next();

})
export default router
